package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.service.MarketTopicService;
import com.cskaoyan.market.service.impl.MarketTopicServiceImpl;
import com.cskaoyan.market.util.GetRequestBodyUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("admin/topic")
public class AdminTopicController {
    @Autowired
    private MarketTopicService marketTopicService;

    @PostMapping("delete")
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetRequestBodyUtil.getRequestBody(req);
        //获取专题Id，根据Id逻辑删除专题
        String id = JacksonUtil.parseString(requestBody, "id");
        marketTopicService.deleteTopic(id);
        String responseBody = JacksonUtil.writeValueAsString(ResponseUtil.ok());
        resp.getWriter().println(responseBody);
    }
    @PostMapping("create")
    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetRequestBodyUtil.getRequestBody(req);
        //将Json格式的字符串转成对应添加专题的专题实体类
        MarketTopic marketTopic = JacksonUtil.getObjectMapper().readValue(requestBody, MarketTopic.class);
        //返回一个装对应返回信息的专题实体类
        MarketTopic resultTopic = marketTopicService.createTopic(marketTopic);
        String respBody = JacksonUtil.writeValueAsString(ResponseUtil.ok(resultTopic));
        resp.getWriter().println(respBody);
    }

    @PostMapping("update")
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetRequestBodyUtil.getRequestBody(req);
        //将Json字符串解析成对应修改条件的专题实体类
        MarketTopic record = JacksonUtil.getObjectMapper().readValue(requestBody, MarketTopic.class);
        //根据专题实体类执行修改
        MarketTopic updatedTopic = marketTopicService.updateTopic(record);
        String asString = JacksonUtil.writeValueAsString(ResponseUtil.ok(updatedTopic));
        resp.getWriter().println(asString);
    }
    @GetMapping("list")
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        //title和subtitle不是必须的请求参数，可能获取为null
        String title = req.getParameter("title");
        if (title == null) {
            title = "";
        }
        String subtitle = req.getParameter("subtitle");
        if (subtitle == null) {
            subtitle = "";
        }
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
//        根据获取参数执行查询
        List<MarketTopic> topics = marketTopicService.getTopics(page, limit, title, subtitle, sort, order);
        String jsonResult = JacksonUtil.writeValueAsString(ResponseUtil.okList(topics));
        resp.getWriter().println(jsonResult);
    }

    @GetMapping("read")
    private void read(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
//        根据Id获取从Service层封装好的对应响应体Json格式的Map容器
        Map<String, Object> result = marketTopicService.getTopicById(id);
        String jsonResult = JacksonUtil.writeValueAsString(ResponseUtil.ok(result));
        resp.getWriter().println(jsonResult);
    }

    //分发请求
    private String getOpString(HttpServletRequest req) {
        String requestURI = req.getRequestURI();
        return requestURI.replace(req.getContextPath() + "/admin/topic/", "");
    }
}
